//
//  TestViewController.m
//  RefreshDemo
//
//  Created by Clavis on 2019/1/9.
//  Copyright © 2019 Clavis. All rights reserved.
//

#import "TestViewController.h"
#import "UIScrollView+SEGSwindmill.h"

@interface TestViewController () <UITableViewDelegate, UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, assign) __block NSInteger dataSource;
@end

@implementation TestViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    self.tableView.backgroundColor = UIColor.blueColor;
    
    self.tableView.contentInset = UIEdgeInsetsMake(20, 0, 30, 0);
    
    [self.tableView addWindmillWithHeaderRefreshBlock:^{
        [self loadData:YES];
    } footerRefreshBlock:^{
        [self loadData:NO];
    }];
}

- (void)loadData:(BOOL)isRefresh {
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self.tableView stopAnimatingWindmill];
        if (isRefresh) {
            self.dataSource = 4;
        } else {
            if (self.dataSource < 15) {
                self.dataSource += 4;
            }
            
        }
    });
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    NSLog(@"%@", self.tableView);
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSource;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.backgroundColor = UIColor.lightGrayColor;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 60;
}

@end
